/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consumo.face.cadastro.impostos;

import consumo.entity.Concessionaria;
import consumo.entity.Icms;
import consumo.entity.TipoUso;
import consumo.face.BaseCDIBean;
import consumo.session.ImpostosSesion;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

/**
 *
 * @author Carlos Taschetto
 */

@ManagedBean
@ViewScoped
public class IcmsFace extends BaseCDIBean<Icms> {

    private static final long serialVersionUID = 1L;
    
    @Inject
    private ImpostosSesion bean;
    private TipoUso selectedTipo;
    private List<Icms> listaI;
    private List<TipoUso> listaT;
    private boolean isNew;
    private List<Concessionaria> listaConce;
    private Icms selectedIcm;
    


    @PostConstruct
    protected void init(){
        System.out.println("init();");
        atribuiEstadoInicial();
    }

    private void atribuiEstadoInicial(){
        listaI = null;
        listaT = null;
        listaConce = null;        
        selectedIcm = new Icms();
        selectedTipo = new TipoUso();
        
        System.out.println("estado inicial!!!!!");
    }

    
    /**
     * inicializa uma nova  a ser gravada
     */
    public void doCreate(){
        System.out.println("Entrou em create Icms");
        setIsNew(true);
        Icms icm = new Icms();
        icm.setConce(listaConce.get(0));
        icm.setTipoUso(listaT.get(0));
        icm.setInicio(new Date());
        setSelectedIcm(icm);
        System.out.println(getSelectedIcm());
    }
    /**
     * Salva a 
     */
    private void doFinishCreate(){
        bean.save(getSelectedIcm());
        listaI = bean.getAll();
    }

    /**
     * Edita uma 
     */
    private void doEdit(){
        bean.set(getSelectedIcm());
        listaI = bean.getAll();
    }
    /**
     * Verifica se é uma nova inserção ou uma alteração
     */
    public void doSave(){
        System.out.println("Entrou doSave: "+isNew);
        System.out.println("Entrou doSave: "+getSelectedIcm());
        System.out.println("Lista: "+listaI);
        System.out.println("getLista -->"+getListaI());
        if(isIsNew())
            doFinishCreate();
        else doEdit();
        setIsNew(false);

        //setSelectedIcm(null);
    }

    public List<Icms> getListaI() {
        if(listaI==null){
            listaI = bean.getAll();
        }
        return listaI;
    }

    public void setListaI(List<Icms> listaI) {
        this.listaI = listaI;
    }

    public List<TipoUso> getListaT() {
        if(listaT==null){
            listaT = bean.getAllTipoUso();
        }
        return listaT;
    }

    public void setListaT(List<TipoUso> listaT) {
        this.listaT = listaT;
    }



    public void doRemove(){
        bean.remove(getSelectedIcm());
        System.out.println("doRemove");
        setListaI(bean.getAll());
    }
    
    public void doRemoveTipoUso(){
        bean.removeEntity(getSelectedTipo());
        setListaT(bean.getAllTipoUso());
    }

    public boolean isIsNew() {
        return isNew;
    }

    public void setIsNew(boolean isNew) {
        this.isNew = isNew;
    }

    public TipoUso getSelectedTipo() {
        return selectedTipo;
    }

    public void setSelectedTipo(TipoUso selectedTipo) {
        this.selectedTipo = selectedTipo;
    }

    /**
     * inicializa uma nova tipo uso a ser gravada
     */
    public void doCreateTipo(){
        System.out.println("Entrou em create tipo;");
        setIsNew(true);
        setSelectedTipo(new TipoUso());
    }

    /**
     * Salva a tipo
     */
    public void doFinishCreateTipo(){
        System.out.println("Salva tipo");
        System.out.println(getSelectedTipo().getDescricao());
        System.out.println(getSelectedTipo().getDesconto());
        System.out.println(getSelectedTipo().getDescontoSegundoPosto());
        if(isIsNew())
            bean.save(getSelectedTipo());
        else bean.set(getSelectedTipo());
        setIsNew(false);
        listaT = bean.getAllTipoUso();
    }

    public List<Concessionaria> getListaConce() {
        if(listaConce==null){
            listaConce = bean.getAllConcessionarias();
            //listaConce.add(0, new Concessionaria());
        }
        return listaConce;
    }

    public void setListaConce(List<Concessionaria> listaConce) {
        this.listaConce = listaConce;
    }

    public Icms getSelectedIcm() {
        System.out.println("get icm: "+selectedIcm);
        return selectedIcm;
    }

    public void setSelectedIcm(Icms selectedIcm) {
        System.out.println("set icm: " +selectedIcm.getConce());
//        for(SelectItem val:getListaConce()){
//            if(val.getValue() != null)
//                if(((Concessionaria)val.getValue()).getId()==selectedIcm.getConce().getId()){
//                    setSelectedConce(val);
//                }
//        }
        this.selectedIcm = selectedIcm;
    }
    
    public void resetIsNew(){
        this.isNew = false;
    }
}
